package model.impl;

import java.util.ArrayList;
import java.util.Collection;

import common.Constants;

import model.LectureServiceListener;
import model.Lesson;
import model.Student;
import model.StudentServiceListener;
import model.StudentService;
import model.datasource.DataSource;
import model.datasource.DataSourceFactory;

public class StudentServiceImpl implements StudentService
{
	private java.util.Collection<StudentServiceListener> listeners;
	private java.util.Collection<Student> students;
	
	public StudentServiceImpl()
	{
		listeners = new ArrayList<StudentServiceListener>();
		students = new ArrayList<Student>();
	}
	public void addNewStudent( String name, String lastName, String nickName, String email, String school, String phoneNumber, int year )
	{
		final String lname = name;
		final String llastname = lastName;
		final String lnickName = nickName;
		final String lemail = email;
		final String lschool = school;
		final String lphoneNumber = phoneNumber;
		final int lyear = year;
		final DataSource ds = DataSourceFactory.getDataSource();
		
		Task t = new Task()
		{
			
			@Override
			public void execute()
			{
				// TODO Auto-generated method stub
				Student s = ds.addNewStudent(lname, llastname, lnickName, lemail, lschool, lphoneNumber, lyear);
				
				java.util.Collection<Student> added = new ArrayList<>();
				java.util.Collection<Student> removed = new ArrayList<>();
				added.add(s);
				
				students.add(s);
				
				notifyStudentsChanged(added, removed);
			}
		};
		
		ServiceDispatcher.getInstance().enqueue(t);
	}
	public void requestAllStudents()
	{
		Task t = new Task()
		{
			
			@Override
			public void execute()
			{
				// TODO Auto-generated method stub
				java.util.Collection<Student> added;
				java.util.Collection<Student> removed = new ArrayList<>();
				
				added = DataSourceFactory.getDataSource().requestAllStudents();
				
				students = added;
				notifyStudentsChanged(added, removed);
			}
		};
		
		ServiceDispatcher.getInstance().enqueue(t);
	}
	
	@Override
	public void addStudentServiceObserver(StudentServiceListener s)
	{
		// TODO Auto-generated method stub
		this.listeners.add(s);
		
	}
	
	@Override
	public void removeStudentServiceObserver(StudentServiceListener s)
	{
		// TODO Auto-generated method stub
		this.listeners.remove(s);
		
	}
	
	@Override
	public void deleteStudent(int id)
	{
		final int lid = id;
		Task t = new Task()
		{
			
			@Override
			public void execute()
			{
				// TODO Auto-generated method stub
				DataSource ds = DataSourceFactory.getDataSource();
				int res = ds.deleteLesson(lid);
				Student studentToRemove = null;
				java.util.Collection<Student> added = new ArrayList<Student>();
				java.util.Collection<Student> removed = new ArrayList<Student>();
				
				if( res == Constants.OK )
				{
					for( Student ss : students )
					{
						if( ss.getId() == lid )
						{
							removed.add(ss);
							studentToRemove = ss;
							break;
						}
					}
					students.remove(studentToRemove);
					
					notifyStudentsChanged(added, removed);
				}
			}
		};
		ServiceDispatcher.getInstance().enqueue(t);
	}	
	
	@Override
	public void updateStudent(int id, String name, String lastName,
			String nickName, String email, String school, String phoneNumber,
			int year)
	{
		// TODO Auto-generated method stub
		final int lid = id;
		final String lname = name;
		final String llastname = lastName;
		final String lnickName = nickName;
		final String lemail = email;
		final String lschool = school;
		final String lphoneNumber = phoneNumber;
		final int lyear = year;
		
		Task t = new Task()
		{
			
			@Override
			public void execute()
			{
				// TODO Auto-generated method stub
				DataSource ds = DataSourceFactory.getDataSource();
				Student s = ds.updateStudent(lid, lname, llastname, lnickName, lemail, lschool, lphoneNumber, lyear);
				
				Collection<Student> added = new ArrayList<Student>();
				Collection<Student> removed = new ArrayList<Student>();
				added.add(s);
				notifyStudentsChanged(added, removed);
			}
		};
		
		ServiceDispatcher.getInstance().enqueue(t);
	}
	
	private void notifyStudentsChanged(Collection<Student>added, Collection<Student>removed)
	{
		for( StudentServiceListener l : listeners )
		{
			l.onStudentsChanged(added, removed);
		}
	}
	
	@Override
	public void deleteStudents(int[] ids)
	{
		// TODO Auto-generated method stub
		throw new RuntimeException("Not implemented");
	}
}
